<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>书籍详情</title>
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/lib/layui-v2.6.3/css/layui.css') }}" media="all">
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/css/layuimini.css') }}" media="all">
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/css/themes/default.css') }}" media="all">
    <link rel="stylesheet" href="{{ url_for('static',filename='layuimini/lib/font-awesome-4.7.0/css/font-awesome.min.css') }}" media="all">
    <style>
        .div-content{
            background-color: white;
            border-radius: 5px;
            padding: 14px;
        }
        .table-search-fieldset{
            margin: 0;
            border: 1px solid #e6e6e6;
            padding: 10px 20px 5px 20px;
            color: #6b6b6b;
        }
        .layui-table-body .layui-table-cell {
            height: 80px;
            line-height: 80px; /* 文本垂直居中 */
            text-align: center;
        }
    </style>
</head>
<body>
<div class="div-content">
    <form class="layui-form layui-form-pane" action="" id="form-search">
        <fieldset class="table-search-fieldset">
            <legend>搜索条件</legend>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 60px">书名</label>
                    <div class="layui-input-inline" style="width: 120px;">
                        <input type="text" name="bookname" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 60px">类型</label>
                    <div class="layui-input-inline" style="width: 100px;">
                        <select name="booksort" lay-verify="">
                            <option value="">请选择</option>
                            <option value="文学">文学</option>
                            <option value="科幻经典">科幻经典</option>
                            <option value="历史">历史</option>
                            <option value="专业书">专业书</option>
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 60px">作者</label>
                    <div class="layui-input-inline" style="width: 120px;">
                        <input type="text" name="bookauthor" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label" style="width: 60px">价格</label>
                    <div class="layui-input-inline" style="width: 80px;">
                        <input type="text" name="startprice" placeholder="最低价" autocomplete="off" class="layui-input">
                    </div>
                    <div class="layui-form-mid">-</div>
                    <div class="layui-input-inline" style="width: 80px;">
                        <input type="text" name="endprice" placeholder="最高价" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <button id="btn-search" lay-submit lay-filter="btn-search-filter" type="button" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal">
                        <i class="layui-icon layui-icon-search"></i>
                        搜索
                    </button>
                    <button id="btn-reset" type="reset" class="layui-btn layui-btn-sm layui-btn-radius layui-btn-primary">
                        <i class="layui-icon layui-icon-fonts-clear"></i>
                        清空条件
                    </button>
                </div>

            </div>
        </fieldset>
    </form>

    <table id="table-booklist" lay-filter="table-booklist-filter"></table>
</div>
<script src="{{ url_for('static',filename='layuimini/lib/layui-v2.6.3/layui.js') }}" charset="utf-8"></script>
<script src="{{ url_for('static',filename='js/jquery-3.6.0.min.js') }}" charset="utf-8"></script>

<script type="text/html" id="table-book-toolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</button>
    </div>
</script>

<script type="text/html" id="col-book-bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>

    function refreshTable(){
        $("#btn-reset").click();
        $("#btn-search").click();
    }

    layui.use(['laydate','table','form'],function () {
        var laydate=layui.laydate;
        var table=layui.table;
        var form=layui.form;

        //表格头部工具栏监听
        table.on('toolbar(table-booklist-filter)', function(obj){
            if(obj.event=="add"){
                layer.open({
                    type: 2,
                    title:"新增图书",
                    area:['40%','90%'],
                    content: '/admin/book/add'
                });
            }else if(obj.event=="delete"){
                var checkStatus = table.checkStatus('table-booklist'); //idTest 即为基础参数 id 对应的值
                console.log(checkStatus.data) //获取选中行的数据
                //没选中数据
                if(checkStatus.data.length==0){
                    layer.alert("您没有选中任何数据！",{icon:2,anim:6,title:"警告"});
                    return false;
                }
                //选中id
                var idArr=new Array();
                $(checkStatus.data).each(function () {
                    idArr.push(this.bookid);
                });
                console.log(idArr.join(","));

                layer.confirm('您确认删除吗?', {icon: 3, title:'删除确认'}, function(index){
                    //异步请求，删除数据
                    $.ajax({
                        url:"/admin/deleteBooks/"+idArr.join(","),
                        success:function (response) {
                            if(response.state==200){
                                layer.msg(response.message,{icon: 1,time:1000});
                                refreshTable();
                            }else {
                                layer.alert("删除失败！",{icon:2,anim:6});
                            }
                        }
                    });
                    layer.close(index);
                });
            }
        });

        //执行一个laydate实例
        laydate.render({
            elem:".date"
        });

        //书籍列表表格渲染
        table.render({
            elem: '#table-booklist'
            ,url: '/admin/querybook' //数据接口
            ,page: true //开启分页
            ,toolbar:"#table-book-toolbar"
            ,limits:[6,8,10,20]
            ,cols: [[ //表头
                {type:"checkbox",width: 60},
                {field: 'bookid', title: '编号', width:70,height:80,align:'center'}
                ,{field: 'bookname', title: '书名', width:120,align:'center'}
                ,{field: 'bookname', title: '图片', width:100,align:'center',
                    templet: function(d){
                        var url="../../../static/image/detail/"+d.bookname+".png";
                        return "<img style='height: 70px' src='"+url+"'>"
                    }
                }
                ,{field: 'booksort', title: '类型', width:90,align:'center'}
                ,{field: 'bookauthor', title: '作者', width:140,align:'center'}
                ,{field: 'bookdinfo', title: '内容',align:'center'}
                ,{field: 'bookprice', title: '价格', width: 80}
                ,{title: '操作', width: 135,align:'center',toolbar: '#col-book-bar'}
            ]]
        });

        //监听表单提交
        form.on('submit(btn-search-filter)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            let substr="";
            let arr={"bookname":data.field.bookname, "booksort":data.field.booksort,"bookauthor":data.field.bookauthor,
                     "startprice":data.field.startprice, "endprice":data.field.endprice};
            for(let i in arr){
                if(arr[i]===""){delete arr[i]}
            }
            for(let j in arr){substr+="&"+j+"="+arr[j];}
            substr=substr.substring(1);
            console.log(substr)

            table.render({
                elem: '#table-booklist'
                ,url: '/admin/querybook?'+substr //数据接口
                ,page: true //开启分页
                ,toolbar:"#table-book-toolbar"
                ,limits:[6,8,10,20]
                ,cols: [[ //表头
                    {type:"checkbox",width: 60},
                    {field: 'bookid', title: '编号', width:70,height:80,align:'center'}
                    ,{field: 'bookname', title: '书名', width:120,align:'center'}
                    ,{field: 'bookname', title: '图片', width:100,align:'center',
                        templet: function(d){
                            var url="../../../static/image/detail/"+d.bookname+".png";
                            return "<img style='height: 70px' src='"+url+"'>"
                        }
                    }
                    ,{field: 'booksort', title: '类型', width:90,align:'center'}
                    ,{field: 'bookauthor', title: '作者', width:140,align:'center'}
                    ,{field: 'bookdinfo', title: '内容',align:'center'}
                    ,{field: 'bookprice', title: '价格', width: 80}
                    ,{title: '操作', width: 135,align:'center',toolbar: '#col-book-bar'}
                ]]
            });
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });

        //操作列
        table.on('tool(table-booklist-filter)', function(obj){
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
             if(layEvent=='del'){
                 layer.confirm('您确认删除该本书吗?', {icon: 3, title:'删除确认'}, function(index){
                     //异步请求，删除数据
                     $.ajax({
                         url:"/admin/deleteBook/"+data.bookid,
                         success:function (response) {
                             if(response.state==200){
                                 layer.msg(response.message,{icon: 1,time:1000});
                                 refreshTable();
                             }else {
                                 layer.alert("删除失败！",{icon:2,anim:6});
                             }
                         }
                     });
                     layer.close(index);
                 });
             }else if(layEvent=='edit'){
                 layer.open({
                     type: 2,
                     title:"修改图书",
                     area:['40%','90%'],
                     content: '/admin/book/find/'+data.bookid
                 });
             }
        });
    });
</script>
</body>
</html>